package com.situ.mall.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.situ.mall.base.entity.Attachment;
import com.situ.mall.base.entity.Category;
import com.situ.mall.base.entity.Product;
import com.situ.mall.base.mapper.AttachmentMapper;
import com.situ.mall.base.mapper.CategoryMapper;
import com.situ.mall.base.mapper.ProductMapper;
import com.situ.mall.base.service.IAttachmentService;
import com.situ.mall.base.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.base.util.AvatarUtil;
import com.situ.mall.base.util.C;
import com.situ.mall.base.vo.IndexProductVO;
import com.situ.mall.base.vo.LayuiTableVO;
import com.situ.mall.base.vo.ResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author darling
 * @since 2023-03-18
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    IAttachmentService attachmentService;

    @Autowired
    AttachmentMapper attachmentMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void doDel(Integer id) {
        baseMapper.doDel(id);
    }

    @Override
    public void doDelAll(Integer[] ids) {
        for (Integer id : ids) {
            doDel(id);
        }
    }

    @Override
    public void doStartOrStop(Integer[] ids, Integer status) {
        for (Integer id : ids) {
            baseMapper.doStartOrStop(id, status);
        }
    }

    @Override
    public ResVO avatar(MultipartFile file) {
        AvatarUtil avatarUtil = new AvatarUtil();
        return avatarUtil.avatar(file, C.PRODUCT_PATH, System.currentTimeMillis() + C.PNG);
    }

    @Override
    public LayuiTableVO doList(String keyword, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return LayuiTableVO.data(baseMapper.doList(keyword, 0));
    }

    @Override
    public LayuiTableVO doDelList(String keyword, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return LayuiTableVO.data(baseMapper.doList(keyword, 1));
    }

    @Override
    public List<IndexProductVO> hanFu() {
        List<IndexProductVO> list = new ArrayList<>();
        // 获取8个最新的分类
        List<Category> categories = categoryMapper.last8(10);
        // 遍历，每个分类中获取最便宜的一款汉服
        for (Category category: categories) {
            // 当前分类中最便宜的
            Product p = baseMapper.cheap(category.getId());
            if (p != null) {
                list.add(new IndexProductVO(p, category));
            }
        }
        return list;
    }


}
